﻿using System.Collections.Concurrent;
using Basic.WxCollect;
using Basic.WxExtendService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Client.Ioc;

namespace Basic.WxExtendService.Extend
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class WxMerCacheService : IWxMerCacheService
    {
        private static readonly ConcurrentDictionary<string, long> _WxMerId = new ConcurrentDictionary<string, long>();

        public long GetWxMerId (string appId)
        {
            if (_WxMerId.TryGetValue(appId, out long id))
            {
                return id;
            }
            using (IocScope scope = RpcClient.Ioc.CreateScore())
            {
                IWxMerCollect wxMer = scope.Resolve<IWxMerCollect>();
                id = wxMer.GetWxMerId(appId);
                _ = _WxMerId.TryAdd(appId, id);
                return id;
            }
        }
    }
}
